<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use Exception;
use think\Log;
use AstroTool\Cache\CacheManage;

class ZlcrAppeal extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_appeal';

    protected $cacheModel = 'astr_admin.astr_appeal';

    /**
     * 申诉
     * @param array $saveData
     * @return bool
     */
    public function saveData(array $saveData): bool
    {
        try
        {
            $this->save([
                'type' => (int)$saveData['type'],
                'qid' => (int)$saveData['qid'],
                'astr_id' => (int)$saveData['astr_id'],
                'reason' => $saveData['reason'],
                'status' => 0,
                'create_time' => $saveData['time']
            ]);
        }
        catch (Exception $e)
        {
            Log::write("占星师申诉失败;错误信息：「{$e->getMessage()}」;错误文件「{$e->getFile()}」;行数「{$e->getLine()}」", true);
            return false;
        }

        return true;
    }

    /**
     * 更新数据
     * @param int $astrId
     * @param $qid
     * @param int $type
     * @param array $data
     * @return bool
     */
    public function updateData(int $appealId, int $astrId, $qid, int $type, array $data)
    {
        try
        {
            $this->where('id', $appealId)->update($data);
            // 删除申诉缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type . ':' . $qid . ':' . $astrId)->rmCache();
        }
        catch (Exception $e)
        {
            Log::write("占星师申诉失败;错误信息：「{$e->getMessage()}」;错误文件「{$e->getFile()}」;行数「{$e->getLine()}」", true);
            return false;
        }

        return true;
    }

    /**
     * 取消申诉
     * @param int $astrId
     * @param  $qid
     * @param int $type
     * @return array|bool|mixed|string
     * @throws Exception
     */
    public function cancelAppeal(int $astrId, $qid, int $type)
    {
        try
        {
            $appealInfo = $this->getAppealInfo($astrId, $qid, $type);
            if ($appealInfo)
            {
                $this->where('id', $appealInfo['id'])->update([
                    'status' => 3, // 取消申诉
                    'update_time' => time()
                ]);
                // 删除申诉缓存
                CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type . ':' . $qid . ':' . $astrId)->rmCache();
            }
        }
        catch (Exception $e)
        {
            Log::write("占星师取消申诉失败;错误信息：「{$e->getMessage()}」;错误文件「{$e->getFile()}」;行数「{$e->getLine()}」", true);
            return false;
        }

        return true;
    }

    /**
     * 获取申诉详情
     * @param int $astrId
     * @param $qid
     * @param int $type
     * @return array|mixed
     * @throws Exception
     */
    public function getAppealInfo(int $astrId, $qid, int $type)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type . ':' . $qid . ':' . $astrId)
            ->getDataAndSet(function () use ($type, $qid, $astrId)
            {
                try
                {
                    $notesData = $this->where(['type' => $type, 'qid' => $qid, 'astr_id' => $astrId])
                        ->whereIn('status', [0, 1, 2])
                        ->order('id desc')
                        ->field('id,reason,record,status,aid,create_time,update_time')
                        ->findOrFail()->toArray();
                }
                catch (Exception $e)
                {
                    $notesData = [];
                }

                return $notesData;
            });
    }
}
